/**
 * easyui 带编辑的表格
 * 
 * @param obj
 *            easyui-datagrid的jQuery对象
 */
function EditTable(obj) {
	this.editIndex = undefined;
	this.rowIndex = -1;
	this.obj = obj;
	this.endEditing=function() {
		var _this = this;
		if (_this.editIndex == undefined) {
			return true
		}
		if (this.obj.datagrid('validateRow', _this.editIndex)) {
			this.obj.datagrid('endEdit', _this.editIndex);
			_this.editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 是否是第一条数据
	 * 
	 * @param dg
	 * @param row
	 * @returns {Boolean}
	 */  
	this.isFirstRow=function(row) {  
	    var index =this.obj.datagrid("getRowIndex", row);  
	    if (index == 0) {  
	        return true;  
	    }  
	    return false;  
	}  
	/**
	 * 是否是最后一条数据
	 * 
	 * @param dg
	 * @param row
	 * @returns {Boolean}
	 */  
	this.isLastRow=function(row) {  
	    var rowNum =this.obj.datagrid("getRows").length;  
	    var index = this.obj.datagrid("getRowIndex", row);  
	    if (index == (rowNum - 1)) {  
	        return true;  
	    }  
	    return false;  
	};
	
	this.getRowindex=function(){
        var selectRowIndex = this.obj.datagrid('getSelectedIndex');
        alert(this.obj.datagrid('getSelectedIndex'));
        if(selectRowIndex == -1){
            this.rowIndex = 0 ;
        }else{
            this.rowIndex = selectRowIndex;
        }
    }
}
/**
 * 向上移动一行
 * 
 * @param dg
 * @param row
 */  
EditTable.prototype.moveUpRow=function() {  
	var _obj = this.obj;
	var _this=this;
	_this.getRowindex();
	alert(_this.rowIndex);
       if(_obj.rowIndex ==0){
           return false;
       }
       var i = --_this.rowIndex;
       if(i>-1){
    	   _obj.datagrid('selectRow',i);
       }else{
    	   _this.rowIndex = 0;
       }
       return false;
}  
/**
 * 向下移动一行
 * 
 * @param dg
 * @param row
 */  
EditTable.prototype.moveDownRow=function(row) {  
	var _obj = this.obj;
    var index = datagrid.datagrid("getRowIndex", row);  
    if (isLastRow(row)) {  
        alert("已经是最后一条！");  
        return;  
    }  
    _obj.datagrid("deleteRow", index);  
    _obj.datagrid("insertRow", {  
        index : index + 1, // 索引从0开始
        row : row  
    });  
    _obj.datagrid("selectRow", index + 1);  
  
}  
/**
 * 点击行实践
 * 
 * @param index
 *            点击的行号
 * @param field
 *            点击的列字段名称
 */
EditTable.prototype.onClickCell = function(index, field) {
	var _obj = this.obj;
	var _this = this;
	if (_this.editIndex != index) {
		if (this.endEditing()) {
			_obj.datagrid('selectRow', index).datagrid('beginEdit', index);
			var ed = _obj.datagrid('getEditor', {
				index : index,
				field : field
			});
			if (ed) {
				($(ed.target).data('textbox') ? $(ed.target).textbox('textbox')
						: $(ed.target)).focus();
			}
			_this.editIndex = index;
		} else {
			setTimeout(function() {
				_obj.datagrid('selectRow', _this.editIndex);
			}, 0);
		}
	}
}
/**
 * 添加
 */
EditTable.prototype.append = function() {
	var _this = this;
	var _obj = _this.obj;
	if (_this.endEditing()) {
		_obj.datagrid('appendRow',{});
		_this.editIndex = _obj.datagrid('getRows').length - 1;
		_obj.datagrid('selectRow', _this.editIndex).datagrid('beginEdit',_this.editIndex);
	}
}
/**
 * 添加
 */
EditTable.prototype.appendOfRow = function(row) {
	var _this = this;
	var _obj = _this.obj;
	if (_this.endEditing()) {
		_obj.datagrid('appendRow',row);
		_this.editIndex = _obj.datagrid('getRows').length - 1;
		_obj.datagrid('selectRow', _this.editIndex).datagrid('beginEdit',_this.editIndex);
	}
}
/**
 * 删除
 */
EditTable.prototype.removeit = function() {
	var _this = this;
	var _obj = _this.obj;
	if (_this.editIndex == undefined) {
		return
	}
	_obj.datagrid('cancelEdit', _this.editIndex).datagrid('deleteRow',
			_this.editIndex);
	_this.editIndex = undefined;
}
/**
 * 保存静态
 */
EditTable.prototype.accept = function() {
	var _this = this;
	var _obj = _this.obj;
	if (_this.endEditing()) {
		_obj.datagrid('acceptChanges');
	}
}